/**
 * 
 */
package com.macbulldev.poc.testgin.jobs;

import javax.servlet.ServletContext;

import net.youngdev.util.Context;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import com.macbulldev.poc.testgin.service.EmailService;
import com.macbulldev.poc.testgin.domain.AppUser;



/**
 * @author solid
 *
 */
@Component("userRequestNotificationJob")
@Configurable
public class UserRequestNotificationJob extends QuartzJobBean 
{
	public static final String USER_OBJ_KEY="USER";

	private static final Logger LOGGER=Logger.getLogger(UserRequestNotificationJob.class);


	@Autowired
	@Qualifier("emailMessageSource")
	private ReloadableResourceBundleMessageSource emailMessages;

	@Autowired
	@Qualifier("emailService")
	private EmailService emailService;


	@Override
	protected void executeInternal(JobExecutionContext context)
	throws JobExecutionException {
		AppUser user = (AppUser)context.getJobDetail().getJobDataMap().get(UserRequestNotificationJob.USER_OBJ_KEY);
		ServletContext ctx =Context.getInstance().getContext();
		//ctx.getAttributeNames();
		String admin = emailMessages.getMessage("application.admin.email",null,null);

		String serverBase = (String)ctx.getAttribute("APPLICATION_BASE_URL");

		context.setResult("Sending email to "+admin);

		try
		{
			LOGGER.debug("User is being enabled "+user);
			String subject = emailMessages.getMessage("email.template.account.request_admin_notification.subject", null, null);
			String body = emailMessages.getMessage("email.template.account.request_admin_notification.body", new Object[]{serverBase,ctx.getContextPath(),user.getEmail(),user.getName(),user.getComment(),user.getPhoneNumber()}, null);
			emailService.sendEmail(new String[]{admin}, subject, body);
				
			subject = emailMessages.getMessage("email.template.account.request_user_notification.subject", null, null);
			body = emailMessages.getMessage("email.template.account.request_user_notification.body", new Object[]{serverBase,ctx.getContextPath()}, null);
			emailService.sendEmail(new String[]{user.getEmail()}, subject, body);
								

			context.setResult("Sending email to "+admin+" complete.");
			Thread.sleep(15000L);


		}
		catch (Exception e)
		{
			LOGGER.error("Exception occured while notifying "+admin,e);
			context.setResult("Sending email to "+admin+" failed.  please review logs. e.message="+e.getMessage());

		}


	}


	public ReloadableResourceBundleMessageSource getEmailMessages() {
		return emailMessages;
	}


	public void setEmailMessages(ReloadableResourceBundleMessageSource emailMessages) {
		this.emailMessages = emailMessages;
	}


	public EmailService getEmailService() {
		return emailService;
	}


	public void setEmailService(EmailService emailService) {
		this.emailService = emailService;
	}

}
